const App=getApp()
Page({
  data: {
    add_img:[],
    images: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that=this
    that.setData({
      order_id:options.id
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  // 选择图片
  chooseImg(e) {
    let that = this
    let add_img =[]
    wx.chooseImage({
      count: 6,
      success: function (res) {
        let tempFilePaths = res.tempFilePaths;
        let uploaderList = that.data.images.concat(tempFilePaths);
        if (uploaderList.length <= 6) {
          that.setData({
            images: uploaderList,
          })
        } else {
          wx.showToast({
            title: '超出张数，请重新上传',
            icon:'none',
            duration:2000
          })
          that.setData({
            images: that.data.images
          })
        }
        for(let i = 0; i<that.data.images.length; i++){
          wx.showLoading({
            title: '上传中',
            mask: true,
          })
          wx.uploadFile({
            url: 'https://jiayou.ziming.work/index.php?s=/api/upload/image',
            filePath: that.data.images[i],
            name: 'iFile',
            header: {
              'content-type': 'multipart/form-data',
            },
            formData: {
              wxapp_id: '10005',
              token: wx.getStorageSync("user_token") || ''
            },
            success: function (res) {
              wx.hideLoading()
              res = JSON.parse(res.data);
              add_img.push(res.data.file_id);
              that.setData({
                add_img:add_img
              })
            },
            fail: function (err) {console.log(err)},
            complete: function (res) { },
          })
        }
      }
    })
  },
  submit(){
    let that=this
    console.log(JSON.stringify(that.data.add_img))
    App.api.$http({
      url: 'Oilorder/staffUpload',
      method: 'GET',
      data: {
        order_id: that.data.order_id,
        image_id:that.data.add_img
      }
    }, {}).then(res => {
      wx.showToast({
        title: res.msg,
        icon:'none',
        duration:2000
      })
      setTimeout(() => {
        wx.navigateBack()
      }, 2000);
    })
  },

  // 删除图片
  delImg(e) {
    let that = this;
    let index = e.currentTarget.dataset.index;
    let imglist=that.data.images
    let imgs = that.data.add_img;
    imgs.splice(index, 1);
    imglist.splice(index, 1);
    that.setData({
      add_img: imgs,
      images:imglist
    })
  },

  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})